package com.sky.service.user.impl;

import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.ShoppingCartDTO;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetMealMapper;
import com.sky.mapper.user.ShoppingCartMapper;
import com.sky.pojo.ShoppingCart;
import com.sky.service.user.ShoppingCartService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetMealMapper setMealMapper;
    /**
     * 添加购物车
     * @param dto
     */
    @Override
    public void add(ShoppingCartDTO dto) {
        //添加购物车，就是往数据库购物车里面新增数据
        //注意逻辑，已经存在的菜品，如果新增，那么只需要修改数量即可
        ShoppingCart shoppingCart = new ShoppingCart();
        //封装数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setDishFlavor(dto.getDishFlavor());
        shoppingCart.setCreateTime(LocalDateTime.now());
        //查询购物车是否存在菜品或套餐
        ShoppingCart existCart=shoppingCartMapper.getByDishOrSetmeal(dto.getDishId(),dto.getSetmealId(),BaseContext.getCurrentId());
        if(existCart!=null){
            log.info("购物车存在该菜品或套餐,修改数量");
            //如果存在，那么只需要修改数量即可
           Integer updateNumber=existCart.getNumber()+1;
            shoppingCartMapper.updateNumber(updateNumber,existCart.getId());
            return;
        }else {
            //如果不存在，那么就新增数据
            shoppingCart.setNumber(1);
        }
        //菜品的情况
        if (dto.getDishId()!=null){
            DishVO dish = dishMapper.getById(dto.getDishId());
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());
            shoppingCart.setDishId(dto.getDishId());
        }else if (dto.getSetmealId()!=null){
            //套餐的情况
            log.info("套餐id:{}",dto.getSetmealId());
            SetmealDTO setmeal = setMealMapper.getById(dto.getSetmealId());
            log.info("套餐信息:{}",setmeal);
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
            shoppingCart.setSetmealId(setmeal.getId());
        }
        //设置金额测试数据
        /*shoppingCart.setAmount(BigDecimal.valueOf(1.0));*/
        shoppingCartMapper.add(shoppingCart);
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        log.info("查看购物车");
        List<ShoppingCart> list=shoppingCartMapper.list(BaseContext.getCurrentId());
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }
}
